import cTitle from "components/title";
import { Toast } from 'vant';
import { scrollMixin } from "utils/mixin";

export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      gold: {},
      second: {},
      three: {},
      kwd: "",
      search: false,
      // point: {
      //   lng: 113.25861276415654,
      //   lat: 23.152827312294523
      // },
      // city: "广州市",

      projectList: [],

      //more
      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  activated() {
    this.init();
    this.setStyle = this.$route.query.setStyle;
    this.ready();
  },
  computed: {
    appointmentLang() {
      if(this.$store.state.shopLanguage.appointment) {
        return this.$store.state.shopLanguage.appointment;
      }else {
        return {worker: "技师", project: "项目", service: "服务"};
      }
    },
  },
  methods: {
    ready() {
      // let myLocation = this.$store.state.o2oLocation;

      // if (myLocation.point) {
      //   this.address = myLocation.title;
      //   this.city = myLocation.city;
      //   this.point = myLocation.point;
      //   //初始化数据
      //   this.getData();
      // } else {
      // console.log("location不存在");
      this.fun.getLocation()
        .then((res)=> {
          this.point = res.point;
          this.address = res.title;
          this.city = res.city;
          this.getData();
        })
        .catch((err)=> {
          console.log(err);
        });
    },
    gotoWorker(id) {
      this.$router.push(this.fun.getUrl("TechnicianDetails", {worker_id: id}));
    },
    init() {
      this.kwd = "";
      this.page = 1;
      this.isLoadMore = false;
      this.total_page = 0;
      this.projectList = [];
      this.search = false;
      this.gold = {};
      this.second = {};
      this.three = {};
    },
    getData() {
      let json = { lng: this.point.lng, lat: this.point.lat, city_name: this.city, kwd: this.kwd, page: 1 };
      if(this.setStyle == 2){
        json.is_nearby = 1;
      }
      $http
        .get(
          "plugin.appointment.frontend.worker.get-workers-by-name",
          json,
          "加载中"
        )
        .then(response => {
          let str = '热门';
          if(this.setStyle == 2){
            str = '附近'
          }else{
            str = '热门'
          }
          this.fun.setWXTitle(str+this.appointmentLang.worker);
          this.kwd ? this.search = true : this.search = false;
          if(this.setStyle == 2){
            this.search = true;
          }
          if (response.result === 1) {
            this.isLoadMore = true;
            this.total_page = response.data.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
            this.projectList = response.data.data;
            this.gold = this.projectList[0];
            this.second = this.projectList[1];
            this.three = this.projectList[2];
            if (!this.second) {
              this.second = {};
            }
            if (!this.three) {
              this.three = {
                name: "null",
                has_one_store: {},
                tags: []
              };
            }
          } else {
            Toast(response.msg);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    //获取更多数据
    getMoreData() {
      const that = this;
      that.isLoadMore = false; // 防止多次请求分页数据
      let json = { lng: this.point.lng, lat: this.point.lat, city_name: this.city, kwd: this.kwd, page: this.page };
      if(this.setStyle == 2){
        json.is_nearby = 1;
      }
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        $http
          .get(
            "plugin.appointment.frontend.worker.get-workers-by-name",
            json,
            "加载中"
          )
          .then(
            function(response) {
              that.isLoadMore = true;
              if (response.result === 1) {
                var myData = response.data.data;
                that.projectList = that.projectList.concat(myData); //数组拼接
              } else {
                that.page = that.page - 1;
                that.isLoadMore = false;
                return;
              }
            },
            function(response) {
              // error callback
            }
          );
      }
    },
    goBack() {
      if (window.history.length <= 1) {
        this.$router.push(this.fun.getUrl("home", {}));
      } else {
        this.$router.go(-1);
      }
    }
  },
  components: { cTitle }
};
